<template>
    <div>
        <img width="300"
            src="https://sanjitian-1256310189.cos.ap-chengdu.myqcloud.com/gin_test/2024/11/2024_11_12_18_27_47_9281_123.jpg"
            alt="" />

        <el-upload v-model:file-list="fileList" class="upload-demo" action="http://goapi.sanjitian.cn/api/files"
            :headers="headers" :data="extraData" name="file" accept="image/*" multiple :on-success="handleAvatarSuccess"
            :show-file-list="false" :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="beforeRemove"
            :on-exceed="handleExceed">
            <el-button type="primary">上传</el-button>
        </el-upload>

        <div class="my-img-list">
            <div v-for="item in myImgList" :key="item.id" class="img-container">
                <img :src="item.filePathCos" :alt="item.fileType" />
                <el-icon class="close-btn" @click="removeImage(item)">
                    <close />
                </el-icon>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Close } from '@element-plus/icons-vue'
import type { UploadProps, UploadUserFile } from 'element-plus'

interface IFileInfo {
    fileExtension: string
    filePathCos: string
    fileSize: string
    fileType: string
    fileUse: string
    id: number
    mineType: string
    uploadTime: string
}
const extraData = {}
const myImgList = ref<IFileInfo[]>([])
const fileList = ref<UploadUserFile[]>([])

const headers = {
    Authorization: `${localStorage.getItem('token')}`
}

const removeImage = (item: IFileInfo) => {
    myImgList.value = myImgList.value.filter(i => i.id !== item.id)
}

const handleRemove: UploadProps['onRemove'] = (file, uploadFiles) => {
    console.log(file, uploadFiles)
}

const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
    console.log(uploadFile)
}

const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
    console.log(uploadFile, '#uploadFile')
    console.log(response.data, '#response')
    myImgList.value.push(response.data)
}

const handleExceed: UploadProps['onExceed'] = (files, uploadFiles) => {
    ElMessage.warning(
        `The limit is 3, you selected ${files.length} files this time, add up to ${files.length + uploadFiles.length
        } totally`
    )
}

const beforeRemove: UploadProps['beforeRemove'] = (uploadFile) => {
    return ElMessageBox.confirm(`Cancel the transfer of ${uploadFile.name} ?`).then(
        () => true,
        () => false
    )
}

</script>

<style lang="scss" scoped>
.my-img-list {
    width: 100%;
    height: 200px;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;

    .img-container {
        position: relative;
        display: inline-block;
        overflow: hidden;
    }

    img {
        height: 150px;
        width: 150px;
    }

    .close-btn {
        font-size: 20px;
        position: absolute;
        top: 5px;
        right: 5px;
        cursor: pointer;
        color: #f56c6c;
        opacity: 0;
        transition: opacity 0.3s;
    }

    .img-container:hover .close-btn {
        opacity: 1;
    }
}
</style>